package com.linzm.leetcode.primary.数组.二分法;

import org.junit.Test;

/**
 * @Author zimingl
 * @Date 2022/10/6 15:14
 * @Description: TODO
 */
public class 数组367有效的完全平方数 {

    public static boolean isPerfectSquare2(int num) {
        int x = (int) Math.sqrt(num);
        return x * x == num;
    }

    public static boolean isPerfectSquare(int num) {
        int left = 0;
        int right = num;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (num > (long) mid * mid) {
                left = mid + 1;
            } else if (num < (long) mid * mid) {
                right = mid - 1;
            } else {
                return true;
            }
        }
        return false;
    }

    private static boolean isPerfectSquare3(int num) {
        int left = 0;
        int right = num;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (num > mid * mid) {
                left = mid + 1;
            } else if (num < mid * mid) {
                right = mid - 1;
            } else {
                return true;
            }
        }
        return false;
    }

    private static boolean isPerfectSquare4(int number){
        int left = 0;
        int right = number;
        while (left >= right){
            int mid  = left + (right - left) / 2;
            if (number > mid * mid){
                left = mid + 1;
            } else if (number > mid * mid) {
                right = mid + 1;
            } else {
                return true;
            }
        }
        return false;
    }

    @Test
    public void test01() {
        int num = 14;
        System.out.println(isPerfectSquare(num));
    }
}
